<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主持人姓名" prop="hostName">
        <el-input
          v-model="queryParams.hostName"
          placeholder="请输入主持人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课题类别" prop="projectType">
        <el-select v-model="queryParams.projectType" placeholder="请选择课题类别" clearable>
          <el-option
            v-for="dict in dict.type.project_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目来源" prop="projectSource">
        <el-select v-model="queryParams.projectSource" placeholder="请选择项目来源" clearable>
          <el-option
            v-for="dict in dict.type.project_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="立项时间">
        <el-date-picker
          v-model="daterangeErpStartTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="预计结题时间">
        <el-date-picker
          v-model="daterangeExpectedEndTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="项目级别" prop="erpProjectLevel">
        <el-select v-model="queryParams.erpProjectLevel" placeholder="请选择项目级别" clearable>
          <el-option
            v-for="dict in dict.type.scg_award_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目评选单位" prop="evaluationUnit">
        <el-input
          v-model="queryParams.evaluationUnit"
          placeholder="请输入项目评选单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否思政课题" prop="isCourseThought">
        <el-select v-model="queryParams.isCourseThought" placeholder="请选择是否思政课题" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['projects:projects:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['projects:projects:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['projects:projects:remove']"
        >删除</el-button>
      </el-col>
      <!-- 审核   -->
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-check"
          size="mini"
          @click="openAuditDialog"
          :disabled="multiple"
          v-hasPermi="['system:check']"
          plain
        >审核</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['projects:projects:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 确保visible是通过sync双向绑定 -->
    <audit-dialog
      :visible.sync="auditDialogVisible"
      table_name="jw_edu_reform_projects"
      id_name="erp_id"
      ref="audit"
      @submit="handleAuditSubmit"
    />


    <el-table v-loading="loading" :data="projectsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="erpId" />
      <el-table-column label="主持人姓名" align="center" prop="hostName" />
      <el-table-column label="教工号" align="center" prop="employeeNumber" />
      <el-table-column label="课题类别" align="center" prop="projectType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目来源" align="center" prop="projectSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_source" :value="scope.row.projectSource"/>
        </template>
      </el-table-column>
      <el-table-column label="立项时间" align="center" prop="erpStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.erpStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计结题时间" align="center" prop="expectedEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目级别" align="center" prop="erpProjectLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.scg_award_level" :value="scope.row.erpProjectLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="项目评选单位" align="center" prop="evaluationUnit" />
      <el-table-column label="课题编号" align="center" prop="erpProjectNumber" />
      <el-table-column label="团队成员" align="center" prop="teamMembers" />
      <el-table-column label="是否思政课题" align="center" prop="isCourseThought">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isCourseThought"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="未通过原因" align="center" prop="rejectionReason" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佐证材料" align="center" prop="wdUplocal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['projects:projects:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['projects:projects:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教育教学改革课题情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1040px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="主持人姓名" prop="hostName">
          <el-input v-model="form.hostName" placeholder="请输入主持人姓名" />
        </el-form-item>
        <el-form-item label="教工号" prop="employeeNumber">
          <el-input v-model="form.employeeNumber" placeholder="请输入教工号" />
        </el-form-item>
        <el-form-item label="课题类别" prop="projectType">
          <el-select v-model="form.projectType" placeholder="请选择课题类别">
            <el-option
              v-for="dict in dict.type.project_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目来源" prop="projectSource">
          <el-select v-model="form.projectSource" placeholder="请选择项目来源">
            <el-option
              v-for="dict in dict.type.project_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="立项时间" prop="erpStartTime">
          <el-date-picker clearable
            v-model="form.erpStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择立项时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计结题时间" prop="expectedEndTime">
          <el-date-picker clearable
            v-model="form.expectedEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计结题时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目级别" prop="erpProjectLevel">
          <el-select v-model="form.erpProjectLevel" placeholder="请选择项目级别">
            <el-option
              v-for="dict in dict.type.scg_award_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目评选单位" prop="evaluationUnit">
          <el-input v-model="form.evaluationUnit" placeholder="请输入项目评选单位" />
        </el-form-item>
        <el-form-item label="课题编号" prop="erpProjectNumber">
          <el-input v-model="form.erpProjectNumber" placeholder="请输入课题编号" />
        </el-form-item>
        <el-form-item label="团队成员" prop="teamMembers">
          <el-input v-model="form.teamMembers" placeholder="请输入团队成员" />
        </el-form-item>
        <el-form-item label="是否思政课题" prop="isCourseThought">
          <el-select v-model="form.isCourseThought" placeholder="请选择是否思政课题">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="佐证材料" prop="wdUplocal">
          <file-upload v-model="form.wdUplocal"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjects, getProjects, delProjects, addProjects, updateProjects } from "@/api/projects/projects";
import AuditDialog from '@/components/AuditDialog/AuditDialog.vue'; // 引入审核子组件

export default {
  name: "Projects",
  dicts: ['scg_award_level', 'project_source', 'sys_yes_no', 'project_type', 'audit_status'],
  components: { AuditDialog },
  data() {
    return {
      // 控制审核对话框的显示
      auditDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教育教学改革课题情况表格数据
      projectsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 佐证材料时间范围
      daterangeErpStartTime: [],
      // 佐证材料时间范围
      daterangeExpectedEndTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hostName: null,
        projectType: null,
        projectName: null,
        projectSource: null,
        erpStartTime: null,
        expectedEndTime: null,
        erpProjectLevel: null,
        evaluationUnit: null,
        isCourseThought: null,
        auditStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hostName: [
          { required: true, message: "主持人姓名不能为空", trigger: "blur" }
        ],
        employeeNumber: [
          { required: true, message: "教工号不能为空", trigger: "blur" }
        ],
        projectType: [
          { required: true, message: "课题类别不能为空", trigger: "change" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        projectSource: [
          { required: true, message: "项目来源不能为空", trigger: "change" }
        ],
        erpStartTime: [
          { required: true, message: "立项时间不能为空", trigger: "blur" }
        ],
        expectedEndTime: [
          { required: true, message: "预计结题时间不能为空", trigger: "blur" }
        ],
        erpProjectLevel: [
          { required: true, message: "项目级别不能为空", trigger: "change" }
        ],
        evaluationUnit: [
          { required: true, message: "项目评选单位不能为空", trigger: "blur" }
        ],
        erpProjectNumber: [
          { required: true, message: "课题编号不能为空", trigger: "blur" }
        ],
        teamMembers: [
          { required: true, message: "团队成员不能为空", trigger: "blur" }
        ],
        isCourseThought: [
          { required: true, message: "是否思政课题不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开审核对话框
    openAuditDialog() {
      if(this.ids!=null)
      {
        this.auditDialogVisible = true; // 显示审核对话框
        this.$refs['audit'].setData(this.ids);
      }
      else
        this.$message.error("请选择要审核的信息");
    },
    // 提交审核数据
    async handleAuditSubmit() {
      this.auditDialogVisible = false; // 关闭审核对话框
      await this.getList(); // 刷新数据列表
    },
    /** 查询教育教学改革课题情况列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeErpStartTime && '' != this.daterangeErpStartTime) {
        this.queryParams.params["beginErpStartTime"] = this.daterangeErpStartTime[0];
        this.queryParams.params["endErpStartTime"] = this.daterangeErpStartTime[1];
      }
      if (null != this.daterangeExpectedEndTime && '' != this.daterangeExpectedEndTime) {
        this.queryParams.params["beginExpectedEndTime"] = this.daterangeExpectedEndTime[0];
        this.queryParams.params["endExpectedEndTime"] = this.daterangeExpectedEndTime[1];
      }
      listProjects(this.queryParams).then(response => {
        this.projectsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        erpId: null,
        hostName: null,
        employeeNumber: null,
        projectType: null,
        projectName: null,
        projectSource: null,
        erpStartTime: null,
        expectedEndTime: null,
        erpProjectLevel: null,
        evaluationUnit: null,
        erpProjectNumber: null,
        teamMembers: null,
        isCourseThought: null,
        auditStatus: null,
        rejectionReason: null,
        userId: null,
        deptId: null,
        updateTime: null,
        wdUplocal: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeErpStartTime = [];
      this.daterangeExpectedEndTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.erpId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教育教学改革课题情况";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const erpId = row.erpId || this.ids
      getProjects(erpId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教育教学改革课题情况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.erpId != null) {
            updateProjects(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjects(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const erpIds = row.erpId || this.ids;
      this.$modal.confirm('是否确认删除教育教学改革课题情况编号为"' + erpIds + '"的数据项？').then(function() {
        return delProjects(erpIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('projects/projects/export', {
        ...this.queryParams
      }, `projects_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
